import createAxiosClient from "@/app/utils/axiosRequest";
import { toast } from "sonner";

// 参数接口定义
export interface CreateCommentParams {
  /*所属笔记ID */
  noteId: number;

  /*评论内容 */
  content: string;
}

/**
 * 创建评论
 * @param params 创建评论请求参数
 */
export async function createComment(params: CreateCommentParams) {
  try {
    const axiosInstance = createAxiosClient();
    const response = await axiosInstance.post(`/app/api/v1/comments`, params);
    toast.success("评论发布成功");
    return response.data;
  } catch (error: any) {
    toast.error(error.message || "评论发布失败");
    throw error;
  }
}

/**
 * 删除评论
 * @param commentId 评论ID
 */
export async function deleteComment(commentId: number) {
  try {
    const axiosInstance = createAxiosClient();
    const response = await axiosInstance.delete(`/app/api/v1/comments/${commentId}`);
    toast.success("评论删除成功");
    return response.data;
  } catch (error: any) {
    toast.error(error.message || "评论删除失败");
    throw error;
  }
}

/**
 * 获取笔记评论数
 * @param noteId 笔记ID
 */
export async function getCommentCount(noteId: number) {
  try {
    const axiosInstance = createAxiosClient();
    const response = await axiosInstance.get(`/app/api/v1/comments/count/${noteId}`);
    return response.data;
  } catch (error: any) {
    toast.error(error.message || "获取评论数失败");
    throw error;
  }
}

/**
 * 获取笔记评论
 * @param noteId 笔记ID
 * @param current 当前页码
 * @param size 每页条数
 */
export async function getNoteComments(noteId: number, current: number = 1, size: number = 10) {
  try {
    const axiosInstance = createAxiosClient();
    const response = await axiosInstance.get(`/app/api/v1/comments/note/${noteId}?current=${current}&size=${size}`);
    return response.data;
  } catch (error: any) {
    toast.error(error.message || "获取笔记评论失败");
    throw error;
  }
}

/**
 * 获取用户评论
 * @param current 当前页码
 * @param size 每页条数
 */
export async function getUserComments(current: number = 1, size: number = 10) {
  try {
    const axiosInstance = createAxiosClient();
    const response = await axiosInstance.get(`/app/api/v1/comments/user?current=${current}&size=${size}`);
    return response.data;
  } catch (error: any) {
    toast.error(error.message || "获取用户评论失败");
    throw error;
  }
} 